package com.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.beans.Admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beans.Book;
import com.mapper.BookMapper;
import com.service.BookService;
import org.springframework.util.StringUtils;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    public List<Book> getBooks(String userName) {
        return bookMapper.getBooks(userName);
    }

    @Override
    public Map<String, Object> queryByPage(Book book, Integer page, Integer limit) {
        Map<String, Object> resMap = new HashMap<>(4);
        PageHelper.offsetPage((page - 1) * limit, limit);
        List<Book> dataList = Collections.emptyList();
        if(StringUtils.hasText(book.getUserName())){
            dataList = bookMapper.getBooks(book.getUserName());
        }else{
            dataList = bookMapper.getAllBook();
        }
        PageInfo<Book> pageInfo = new PageInfo<>(dataList);
        resMap.put("code", 0);
        resMap.put("msg", "查询成功");
        resMap.put("data", pageInfo.getList());
        resMap.put("count", pageInfo.getTotal());
        return resMap;
    }

    public int addBook(Book Book) {
        return bookMapper.addBook(Book);
    }

    public int deleteBook(int id) {
        return bookMapper.deleteBook(id);
    }

    public Book getBookById(int id) {
        return bookMapper.getBookById(id);
    }

    public List<Book> getAllBook() {
        return bookMapper.getAllBook();
    }

    public Integer updateBook(Book Book) {
        return bookMapper.updateBook(Book);
    }
}


